package com.example.vote.flow.controller;

import com.example.vote.flow.pojo.core.Principle;
import com.example.vote.flow.pojo.param.VoteOptionAddNewParam;
import com.example.vote.flow.pojo.param.VoteOptionUpdateParam;
import com.example.vote.flow.pojo.vo.VoteOptionFullVO;
import com.example.vote.flow.service.IVoteOptionService;
import com.example.vote.flow.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "3. 投票选项管理模块")
@RestController
@RequestMapping("/api/vote-option")
public class VoteOptionController {

    @Autowired
    private IVoteOptionService voteOptionService;

    @ApiOperationSupport(order = 100)
    @Operation(summary = "[用户] 新增投票选项")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@ParameterObject VoteOptionAddNewParam param, HttpSession session) {
        Principle principle = (Principle) session.getAttribute(Principle.SESSION_KEY);
        voteOptionService.addNew(param, principle);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 200)
    @Operation(summary = "[用户] 删除投票选项")
    @Parameter(name = "id", description = "投票选项id", required = true, example = "1")
    @PostMapping("/{id}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id, HttpSession session) {
        Principle principle = (Principle) session.getAttribute(Principle.SESSION_KEY);
        voteOptionService.deleteById(id, principle);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 300)
    @Operation(summary = "[用户] 编辑投票选项")
    @Parameter(name = "id", description = "投票选项id", required = true, example = "1")
    @PostMapping("/{id}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, @ParameterObject VoteOptionUpdateParam param, HttpSession session) {
        Principle principle = (Principle) session.getAttribute(Principle.SESSION_KEY);
        voteOptionService.updateById(id, param, principle);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 411)
    @Operation(summary = "[游客] 根据ID查询投票选项完整信息")
    @Parameter(name = "id", description = "投票选项id", required = true, example = "1")
    @GetMapping("/public/{id}/full-info")
    public JsonResult<VoteOptionFullVO> selectFullById(@PathVariable Long id) {
        VoteOptionFullVO data = voteOptionService.selectFullInfoById(id);
        return JsonResult.ok(data);
    }

}
